package com.xes.attend.scheduler.impl;


import com.xes.attend.config.ServerConfig;
import com.xes.attend.rd60n.jiecard.cn.RD60N;
import com.xes.attend.scheduler.Scheduler;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

import java.util.Calendar;

@Component
public class RfMachineSchedulerService implements Scheduler {
    @Autowired
    private ServerConfig config;
    private static final Logger log = LogManager.getLogger(RfMachineSchedulerService.class);

    @Override
    @Scheduled(cron = "0 */30 * * * *")        // 每30分钟
    public void execute() {
        StopWatch stopWatch = new StopWatch("校正读卡机时间服务:");
        for (String ip : config.getCardReaderIp()) {
            stopWatch.start("读卡机ip[:" + ip + "]");
            RD60N dev = new RD60N();
            dev.rf_open(ip, "UDP");

            Calendar c = Calendar.getInstance();
            short[] datetime = {(short) (c.get(Calendar.YEAR)), (short) (c.get(Calendar.MONTH) + 1),
                    (short) (c.get(Calendar.DATE)), (short) (c.get(Calendar.HOUR_OF_DAY)),
                    (short) (c.get(Calendar.MINUTE)), (short) (c.get(Calendar.SECOND))};

            log.info("正在校正读卡器 :{},时间:{}", ip, c.getTime().toString());

            dev.rf_set_datetime(datetime);
            dev.rf_beep((byte) 5);
            dev.rf_close();
            stopWatch.stop();
        }
		log.debug(stopWatch.prettyPrint());
    }
}
